#include "configwindow.h"
#include "ui_configwindow.h"

#include "QDebug"
#include "QDir"
#include "QIcon"
#include <phonon/audiooutput.h>
#include <phonon/mediaobject.h>

ConfigWindow::ConfigWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ConfigWindow)
{
    ui->setupUi(this);
    this->timerState = ConfigWindow::None;

    //notifies every 1 second
    labelUpdater = new QTimer(this);
    connect(labelUpdater, SIGNAL(timeout()), this, SLOT(updateEverySecond()));

    this->createTrayIcon();
    this->updateIcon();
    trayIcon->show();


}

ConfigWindow::~ConfigWindow()
{
    delete ui;
}


void ConfigWindow::stopTimer()
{
    qDebug() << "Stop timer";
    timer->stop();
    this->timerState = ConfigWindow::None;
    this->updateEverySecond();
}

void ConfigWindow::startTimer()
{


    labelUpdater->start(1000);


    //flow
    //1. count pomodoro time
    //2. when pomodoro time is done, ring
    //3. count pause time
    //4. when pause time is done, ring and stop



    pomodoroSecs = this->ui->pomodoroSpinBox->value()*60;
    pauseSecs    = this->ui->pauseSpinBox->value() *60;

    this->timerState = ConfigWindow::Pomodoro;
    this->remainingSecs = pomodoroSecs;

    this->updateEverySecond();

    qDebug() << "Starting pomodoro...";
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(timeoutPomodoro()));
    timer->setSingleShot(true);
    timer->start(pomodoroSecs *1000  );
    qDebug() << "Pomodoro Started!";

    this->updateIcon();
}


void ConfigWindow::updateEverySecond()
{



    QTime t;
    QString time = t.addSecs(this->remainingSecs).toString("m:ss");
    qDebug()<<"Updating time remaining:" << time;


    this->controlButtonsEnabling();


    switch(this->timerState)
    {
        case ConfigWindow::Pomodoro:
            this->ui->timeRemainingLabel->setText(time + tr(" remaining [pomodoro]"));
            break;
        case ConfigWindow::Pause:
            this->ui->timeRemainingLabel->setText(time + tr(" remaining [pause]"));
            break;
        default :
            this->ui->timeRemainingLabel->setText(tr("---"));
            labelUpdater->stop();
            break;
    }

    remainingSecs = remainingSecs == 0? 0 : remainingSecs-1;

}


void ConfigWindow::controlButtonsEnabling()
{
    this->ui->stopButton->setEnabled(this->timerState != ConfigWindow::None);
    this->ui->startButton->setEnabled(this->timerState == ConfigWindow::None);
}

void ConfigWindow::timeoutPomodoro()
{

    this->playAlert();


    //estados e timers
    this->timerState = ConfigWindow::Pause;
    this->remainingSecs = pauseSecs;



    qDebug() << "Starting pause...";
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(timeoutPause()));
    timer->setSingleShot(true);
    timer->start(pauseSecs * 1000);
    qDebug() << "Pause Started!";

    this->updateIcon();

}


void ConfigWindow::timeoutPause()
{

    //estados e timers
    this->timerState = ConfigWindow::None;
    this->remainingSecs = 0;
    timer->stop();
    qDebug() << "Default state";
    this->updateIcon();

}


void ConfigWindow::playAlert()
{
    if (this->ui->enableSoundCheckBox->isChecked())
    {
        QDir dir(QCoreApplication::applicationDirPath());

        QString soundFile;
        soundFile = dir.absolutePath();
        soundFile += QDir::separator();
        soundFile += "sounds";
        soundFile += QDir::separator();
        soundFile += "timerexpired.ogg" ;

        Phonon::MediaObject *music =
                 Phonon::createPlayer(Phonon::MusicCategory,
                                      Phonon::MediaSource(soundFile));
        music->play();
    }

}

void ConfigWindow::createTrayIcon()
{
    trayIcon = new QSystemTrayIcon(this);
}


void ConfigWindow::updateIcon()
{

    QIcon icon;

    switch(this->timerState)
    {
        case ConfigWindow::Pomodoro:
            icon = QIcon(":/images/tomato.svg");
            break;
        case ConfigWindow::Pause:
            icon = QIcon(":/images/coffee.svg");
            break;
        default :
            icon = QIcon(":/images/tomato_bw.svg");
            break;
    }

    trayIcon->setIcon(icon);
    setWindowIcon(icon);
}
